package com.hup.joystick.presenter;

import com.hup.joystick.config.AppConfig;
import com.hup.joystick.model.DeviceConfig;
import com.hup.joystick.mvp.JoySettingMvp.JoySettingPresenter;
import com.hup.joystick.mvp.MainMvp.MainPresenter;
import com.hup.joystick.service.jna.DInputHResultException;
import com.hup.joystick.service.jna.EnumDeviceData;
import com.hup.joystick.service.joystick.JoyEventListener;
import com.hup.joystick.service.joystick.JoystickService;
import com.hup.joystick.view.joySetting.JoySettingFrame;
import com.hup.utils.commons.io.BaseConfig.Path;
import com.hup.utils.commons.start.SpringContext;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Objects;
import java.util.Vector;

/**
 * @author hugan
 * @date 2020/10/19
 */
@Log4j2
@Lazy
@Component
public class JoySettingPresenterImpl implements JoySettingPresenter {

    private final JoystickService joystickService;

    public JoySettingPresenterImpl(JoystickService joystickService) {
        this.joystickService = joystickService;
    }

    /**
     * 合设备列表:当前设备,有配置文件的离线设备.按设备名排序
     */
    @Override
    public Vector<DeviceConfig> getDevices() {
        log.info("");
        Vector<DeviceConfig> res = new Vector<>();
        //已配置的设备
        File configDir = new File(Path.getBaseDir());
        File[] files = configDir.listFiles((dir, name) -> name.endsWith(DeviceConfig.EXT));
        if (files != null) {
            for (File file : files) {
                DeviceConfig data = DeviceConfig.loadFromFile(file);
                if (data != null) res.add(data);
                else log.warn("配置文件异常={}", file.getAbsolutePath());
            }
        }
        //在线设备
        try {
            ArrayList<EnumDeviceData> lstOnline = joystickService.enumDevices();
            F1:
            for (EnumDeviceData online : lstOnline) {
                for (DeviceConfig config : res) {
                    if (Objects.equals(config.name, online.tszInstanceName) && Objects.equals(config.guid, online.guid)) {
                        config.online = true;
                        continue F1;
                    }
                }
                //新在线设备
                DeviceConfig config = new DeviceConfig(online.tszInstanceName, online.guid);
                config.online = true;
                res.add(config);
            }
            res.sort(Comparator.comparing(o -> o.name));
        } catch (DInputHResultException e) {
            res.sort(Comparator.comparing(o -> o.name));
            DeviceConfig errItem = new DeviceConfig(e.getMessage(), "");
            errItem.err = e;
            res.add(0, errItem);
        }

        log.info("res.size={}", res.size());
        //res.clear();
        return res;
    }

    @Override
    public void startListenState(String guid, JoyEventListener joyEventListener) {
        joystickService.startListenState(guid, 1000 / AppConfig.STATE_SCAN_FPS, AppConfig.STICK_RANGE, joyEventListener);
    }

    @Override
    public void stopListenState(boolean waitEndingThread) {
        joystickService.stopListenState(waitEndingThread);
    }

    @Override
    public void onWindowClosing() {
        log.info("");
        joystickService.stopListenState(true);
        SpringContext.getBean(MainPresenter.class).onWindowClosing(JoySettingFrame.class);
    }
}
